package util.crypto

import javax.crypto.{CipherInputStream, CipherOutputStream, Cipher}
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, InputStream, OutputStream}
import java.nio.charset.Charset
import java.security.{Key, PrivateKey, PublicKey, KeyFactory}
import java.security.spec.{KeySpec, EncodedKeySpec, PKCS8EncodedKeySpec, X509EncodedKeySpec}

/**
 * Inspired by http://coding.westreicher.org/?p=23
 */


class RSADecoder(keyStream: InputStream, usePrivate: Boolean) extends RSACoderBase(keyStream, Cipher.DECRYPT_MODE, usePrivate) {

  def this(keyBytes: Array[Byte], usePrivate: Boolean) = this (new ByteArrayInputStream(keyBytes), usePrivate)

  def decrypt(bytes: Array[Byte], enc: Charset): String = {
    val decrypted = blockCipher(bytes)
    new String(decrypted, enc)
  }

  def decrypt(input: InputStream, output: OutputStream) {
    val bos = new ByteArrayOutputStream
    copy(input, bos)
    bos.close
    val decrypted = blockCipher(bos.toByteArray)
    output.write(decrypted)
    output.flush
  }

}





